﻿using System;
using Utils.Wpf;

namespace StockAnalyst2.StockGrid
{
	internal class OptionsManager
	{
		#region Properties

		public AllColumnsOptions AllColumnsOptions { get; private set; }

		#endregion

		#region Events

		public event EventHandler<DialogShowRequestedEventArgs> DialogShowRequested;

		#endregion

		#region Public Methods

		public OptionsManager()
		{
			this.AllColumnsOptions = AllColumnsOptionsDataSource.Load();
		}

		public void ShowOptions()
		{
			OptionsDialog optionsDialog = new OptionsDialog();
			using (OptionsDialogVM viewModel = new OptionsDialogVM(this.AllColumnsOptions))
			{
				optionsDialog.DataContext = viewModel;

				DialogShowRequestedEventArgs eventArgs = new DialogShowRequestedEventArgs(optionsDialog);
				this.OnDialogShowRequested(eventArgs);
				bool? dialogResult = eventArgs.DialogResult;

				if (dialogResult != null && dialogResult.Value && optionsDialog.DataContext.DataChanged)
				{
					this.AllColumnsOptions.Copy(optionsDialog.DataContext.AllColumnsOptionsVM.Model);
					AllColumnsOptionsDataSource.Save(this.AllColumnsOptions);
					Properties.Settings.Default.Save();
				}
			}
		}

		protected virtual void OnDialogShowRequested(DialogShowRequestedEventArgs e)
		{
			if (this.DialogShowRequested != null)
			{
				this.DialogShowRequested(this, e);
			}
		}

		#endregion
	}
}
